var pageNow=1;
SPA_RESOLVE_INIT = function(transition) {
    var html = '<div class="commonRight-top-select mb-10 f-14 mt-15">\n' +
        '        <a href="javascript:;" id="btn-showaddUser" class="ui-btn text-center rad-3 f-14 ml-10" onclick="showaddUser(this)" data-show="false" >添加</a>\n' +
        '        <div id="addUser" style="display: inline">' +
        '           <input type="number" id="input-adduser" class="rad-3 ml-10 f-14">' +
        '           <a href="javascript:;" class="ui-btn text-center c-fff f-14 bg-169 rad-3 ml-10 " onclick="addleader()">确认添加</a>'+
        '        </div>\n'+
        '    </div>\n' +
        '    <table class="Table_common newsListTab mt-20">\n' +
        '        <thead>\n' +
        '        <tr>\n' +
        // '            <td>课程</td>\n' +
        '            <td>工号</td>\n' +
        // '            <td>姓名</td>\n' +
        '            <td>密码</td>\n' +
        '            <td>电话</td>\n' +
        '            <td>邮箱</td>\n' +
        // '            <td>密码</td>\n' +
        '            <td>操作</td>\n' +
        '        </tr>\n' +
        '        </thead>\n' +
        '        <tbody id="newsListTabs">\n' +
        '        </tbody>\n' +
        '    </table>\n' +
        '    <div class="paging-area-box mt-10 magin-auto">\n' +
        '        <div class="paging-area float-left" id="pagination"></div>\n' +
        '        <span class="pageNum float-right f-14">共<em>0</em>条数据</span>\n' +
        '    </div>';
    html += '<style>\n' +
        '\n' +
        '        .newsListTab tr td:nth-child(2),\n' +
        '        .newsListTab tr td:nth-child(4){\n' +
        '            border-left:1px solid #333;\n' +
        '            border-right:1px solid #333;\n' +
        '        }\n' +
        '        .newsListTab tr td:nth-child(5){\n' +
        '            border-right:1px solid #333;\n' +
        '        }\n' +
        '        .commonRight-top-select{\n' +
        '            margin-left:2%;\n' +
        '        }\n' +
        '        .commonRight-top-select select{\n' +
        '            border-radius: 3px;\n' +
        '            border:1px solid #999;\n' +
        '            width:125px;\n' +
        '            height:30px;\n' +
        '        }\n' +
        '        .commonRight-top-select a.ui-btn{\n' +
        '            padding:3px 8px;\n' +
        '            border:1px solid #666;\n' +
        '        }\n' +
        '\n' +
        '    </style>';
    getHref_path();
    $(".menu ul").hide();
    $(".menu ul").eq(5).show();
    $("#ui-right-content").html(html);
    $("#addUser").hide();
    leaderListFun(pageNow)
}

function leaderListFun(pageNo){
    var id = window.document.getElementById("newsListTabs");
    $.ajax({
        url: get_headLlist_interface+'?currentPage='+pageNo,
        type:'post',
        dataType:'json',
        xhrFields:{
            withCredentials:true
        },
        crossDomain:true,
        data:{
            currentPage:pageNo
        },
        success: function(data) {
            if(data.code==200){
                inner_leaderList_Tbody(data,id, leaderListFun);
            }else{
                $.message(data.msg);
            }
        },
        error: function (error) {
            $.message("链接超时，请刷新...");
        }
    });
}

function inner_leaderList_Tbody(data,id, func){
    var page = data.data.num;
    var currentPage = data.data.currentPage;
    var tml = "";
    $(".pageNum em").html(page);
    if(page>0){
        setPagination(page,currentPage, func);
        pageNow=currentPage;
        var lists = data.data.list;
        $('#pagination').show();
        for (x in lists) {
            if(lists[x]["email"]==null)lists[x]["email"]='空';
            if(lists[x]["telephone"]==null)lists[x]["telephone"]='空';
            tml += "<tr>"+
                // '<td>'+x+'</td>'+
                '<td>'+lists[x]["username"]+'</td>'+
                "<td>"+lists[x]["nickname"]+"</td>"+
                "<td>"+lists[x]["telephone"]+"</td>"+
                "<td>"+lists[x]["email"]+"</td>"+
                '<td><a href="javascript:;" class="c-009 f-14" onclick="resetUser(this)" data-username="'+lists[x]["username"]+'">重置密码</a>  /' +
                '  <a href="javascript:;" class="c-009 f-14" onclick="deleteUser(this)" data-username="'+lists[x]["username"]+'">删除</a></td>'+
                "</tr>" ;
        }
    }else{
        $('#pagination').hide();
    }
    id.innerHTML = tml;
}

/**编辑**/
function resetUser(obj){
    var username = $(obj).attr("data-username");
    $.ajax({
        url: reset_interface,
        type:'post',
        dataType:'json',
        xhrFields:{
            withCredentials:true
        },
        crossDomain:true,
        data:{
            resetUsername:username,
        },
        success: function(data) {
            if(data.code==200){
                $.message("重置成功");
                leaderListFun(pageNow)
            }else{
                $.message(data.msg);
            }
        },
        error: function (error) {
            $.message("链接超时，请刷新...");
        }
    });
}
/**删除***/
function deleteUser(obj){
    var username = $(obj).attr("data-username");
    $.ajax({
        url: delete_user_interface,
        type:'post',
        dataType:'json',
        xhrFields:{
            withCredentials:true
        },
        crossDomain:true,
        data:{
            username:username,
        },
        success: function(data) {
            if(data.code==200){
                $.message("删除成功");
                // $(obj).closest("tr").remove();
                leaderListFun(pageNow)
            }else{
                $.message(data.msg);
            }
        },
        error: function (error) {
            $.message("链接超时，请刷新...");
        }
    });
}

// 显示添加用户的版面
function showaddUser(that) {
    $("#addUser").toggle();
    $("#input-adduser").val("");
    if($(that).html()=="添加") {
        that.innerHTML = "取消";
    }
    else {
        that.innerHTML = "添加";
    }
}

//添加用户
function addleader() {
    var addUsername = $("#input-adduser").val();
    $.ajax({
        url: register_head_teacher_interface,
        type:'post',
        dataType:'json',
        xhrFields:{
            withCredentials:true
        },
        crossDomain:true,
        data:{
            username:addUsername
        },
        success: function(data) {
            if(data.code==200){
                $.message("添加成功");
                leaderListFun(pageNow)

            }else{
                $.message(data.msg);
            }
        },
        error: function (error) {
            $.message("链接超时，请刷新...");
        }
    });
}